﻿// <copyright file="DateTimeOffset.Extensions.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-07-18</date>
// <summary>DateTimeOffset extensions</summary>

namespace Webtrends.WebAnalytics
{
    using System;
    using System.Globalization;

    /// <summary>
    /// DateTimeOffset extension methods
    /// </summary>
    public static class DateTimeOffsetExtensions
    {
        /// <summary>
        /// Generate a UNIX timestamp (number of milliseconds since 1/1/1970)
        /// </summary>
        /// <param name="dateTime">a date to convert</param>
        /// <returns>a timestamp in seconds since January 1, 1970</returns>
        public static string ToUnixTimestamp(this DateTimeOffset dateTime)
        {
            // Default implementation of UNIX time of the current UTC time
            TimeSpan ts = dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0);

            return Convert.ToUInt64(ts.TotalMilliseconds).ToString(CultureInfo.InvariantCulture);
        }
    }
}
